<?php

namespace App\Http\Controllers\Database;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;


class BaseController extends Controller
{

    // 获取数据库

    // 初始化操作
    public function initialize() { }


    // 获取数据表信息
    public function getTables($db_name = '')
    {
        // 如果没有指定数据库  则获取默认的数据库
        if(empty($db_name)){
            $db_name = env('DB_DATABASE');
        }

        $do_sql = "SELECT `TABLE_NAME` tablename, `ENGINE` engine,TABLE_COMMENT table_comment FROM information_schema.TABLES WHERE TABLE_SCHEMA='".$db_name."'";
        $tables = DB::select($do_sql);

        // 将对象转换成数组
        $tables = $this->arrayObjectToArray($tables);


        return $tables;
    }


    // 获取整个数据表的字段信息
    protected function getFields($db_name = '', $table = '', $where = '')
    {
        // 如果没有指定数据库  则获取默认的数据库
        if(empty($db_name)){
            $db_name = env('DB_DATABASE');
        }

        $do_sql = "SELECT `TABLE_NAME` tablename, `COLUMN_NAME` field,DATA_TYPE `type`,IS_NULLABLE is_null ,CHARACTER_MAXIMUM_LENGTH  `length`,NUMERIC_PRECISION num_length,NUMERIC_SCALE num_bit,COLUMN_COMMENT comment,COLUMN_DEFAULT `default` FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='".$db_name."'";
        if($table){
            $do_sql .= " AND TABLE_NAME='".$table."' ";
        }
        if($where){
            $do_sql .= '  AND ( '.$where.' ) ';
        }

        $fields = Db::select($do_sql);
        // 将对象转换成数组
        $fields = $this->arrayObjectToArray($fields);

        return $fields;
    }


    // 展示字段出现次数
    protected function showTimesHtml($fields)
    {
        // 出现次数统计
        $keys   = array_column($fields, 'field');
        $times  = array_count_values($keys);
        $tables = count(array_keys(array_column($fields, null, 'tablename')));

        // 页面展示
        $style = "<style>table, th, td {border: 1px solid black;}</style>";
        $style = "<style>table, th, td {border: 1px solid #0a6aa1;}</style>";

        $html = '<tr><td>数据表</td><td>字段</td><td>类型</td><td>是否为空</td><td>长度</td><td>数字长度</td><td>字节数</td><td>字段名</td><td>默认值</td><td>出现次数</td><tr>';
        foreach($fields as $_field){
            $_time = $times[$_field['field']];
            $color = $_time < $tables ? 'red' : '';
            $html  .= '<tr style="background-color: '.$color.'"><td>'.$_field['tablename'].'</td><td>'.$_field['field'].'</td><td>'.$_field['type'].'</td><td>'.$_field['is_null'].'</td><td>'.$_field['length'].'</td><td>'.$_field['num_length'].'</td><td>'.$_field['num_bit'].'</td><td>'.$_field['comment'].'</td><td>'.$_field['default'].'</td><td>'.$_time.'</td><tr>';
        }

        return $style.'<table>'.$html.'</table>';
    }

    // 数组对象转换成数组
    protected function arrayObjectToArray($tables)
    {
        // 将对象转换成数组
        if($tables){
            $lists = [];
            foreach($tables as $table){
                $lists[] = get_object_vars($table);
            }

            $tables = $lists;
        }

        return $tables;
    }

    // 换行
    protected function cline()
    {
        return chr(10).'<br/><br/>'.chr(10).chr(10);
    }

    // 输出
    protected function showMsg($msg, $var = '')
    {
        if($msg){
            echo '<h3>'.$msg.'：</h3>';
        }
        if($var){
            if(is_array($var)){
                var_dump($var);
            } else{
                echo $var;
            }
            echo '<br><br>';
        }
    }


}
